﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DigtiMatrix.Entity;

namespace DigitMatrix.DataAccess
{
    public class CamercaDataAccess
    {
        public static int GetMaxCamercaId(Database db)
        {
            string cmdText = "select max(CamercaId) from CamercaInfo";
            try
            {
                return int.Parse(db.ExecuteScalar(CommandType.Text, cmdText).ToString());

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        private const string INSERT_CAMERCA = "INSERT INTO CamercaInfo()";
        public static DataSet SearchVideoFileName(Database db,int eventType,  int camercaId, DateTime dStartTime, DateTime dEndDate)
        {
            string cmdText = "select * from SysEvent where 1=1";
            if (eventType != 0)
            {
                cmdText = cmdText + " AND EventType=" + eventType.ToString(); 
            }
            if (camercaId != 0)
            {
                cmdText = cmdText + " AND camercaId=" + camercaId.ToString();
            }
            cmdText = cmdText +string.Format( " AND EventDate>='{0}' and EventDate<='{1}'",dStartTime.ToString("yyyy-MM-dd HH:mm:ss"),dEndDate.ToString("yyyy-MM-dd HH:mm:ss")) ;
            try
            {
                return db.ExecuteDataSet(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }
             

        }
     
        public static int Insert(Database db, CamercaInfo oCamercaInfo)
        {
            StringBuilder sbField = new StringBuilder();
            StringBuilder sbValue = new StringBuilder();
            sbField.Append("INSERT INTO CamercaInfo(");
            sbValue.Append("values(");
            sbField.Append("DeviceId");
            sbValue.AppendFormat("{0}", oCamercaInfo.DeviceId);
            sbField.Append(",Name");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.Name);
            sbField.Append(",Description");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.Description);
            sbField.Append(",IsValid");
            sbValue.AppendFormat(",{0}", oCamercaInfo.IsValid);
            sbField.Append(",ChannelNo");
            sbValue.AppendFormat(",{0}", oCamercaInfo.ChannelNo);
            sbField.Append(",AddressID");
            sbValue.AppendFormat(",{0}", oCamercaInfo.AddressID);
            sbField.Append(",ConnURL");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.ConnURL);
            sbField.Append(",Remark");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.Remark);
            sbField.Append(",Oupputpath");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.Oupputpath);
            sbField.Append(",frameInterval");
            sbValue.AppendFormat(",{0}", oCamercaInfo.frameInterval);

            sbField.Append(",resolution");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.resolution);

            sbField.Append(",quality");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.quality);


            sbField.Append(",StreamType");
            sbValue.AppendFormat(",{0}", oCamercaInfo.StreamType);

          

            sbField.Append(",AddBy");
            sbValue.AppendFormat(",'{0}'", oCamercaInfo.AddBy);
            sbField.Append(",AddTime)");
            sbValue.AppendFormat(",'{0}')", oCamercaInfo.AddTime);
            string cmdText = sbField.ToString() + " " + sbValue.ToString()  ;
            try
            {
                return  db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static int Update(Database db, CamercaInfo oCamercaInfo)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update CamercaInfo set");
            sb.AppendFormat(" Name='{0}'", oCamercaInfo.Name);
            sb.AppendFormat(",Description='{0}'", oCamercaInfo.Description);
            sb.AppendFormat(",IsValid={0}", oCamercaInfo.IsValid);
            sb.AppendFormat(",ChannelNo={0}", oCamercaInfo.ChannelNo);
            sb.AppendFormat(",AddressID={0}", oCamercaInfo.AddressID);
            sb.AppendFormat(",ConnURL='{0}'", oCamercaInfo.ConnURL);
            sb.AppendFormat(",Remark='{0}'", oCamercaInfo.Remark);
            sb.AppendFormat(",Oupputpath='{0}'", oCamercaInfo.Oupputpath);
            sb.AppendFormat(",ModifyBy='{0}'", oCamercaInfo.AddBy);
            sb.AppendFormat(",ModifyTime='{0}'", oCamercaInfo.AddTime);
            sb.AppendFormat(",frameInterval={0}", oCamercaInfo.frameInterval);
            sb.AppendFormat(",resolution='{0}'", oCamercaInfo.resolution);
            sb.AppendFormat(",quality='{0}'", oCamercaInfo.quality);
            sb.AppendFormat(",StreamType={0}", oCamercaInfo.StreamType);
            sb.AppendFormat(" where CamercaId={0}", oCamercaInfo.CamercaId);
            string cmdText = sb.ToString();
            try
            {
                return db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }


        }
        public static int Delete(Database db, int CamercaId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from CamercaInfo ");
            sb.AppendFormat(" where CamercaId={0}", CamercaId);
            string cmdText = sb.ToString();
            try
            {
                return db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public static int DeleteByDeviceId(Database db, int DeviceId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from CamercaInfo ");
            sb.AppendFormat(" where DeviceId={0}", DeviceId);
            string cmdText = sb.ToString();
            try
            {
                return db.ExecuteNonQuery(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }


        public static DataSet GetAllCamInfo(Database db)
        {
            string cmdText = string.Format("select * from CamercaInfo order by CamercaId");
            try
            {
                return db.ExecuteDataSet(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static DataSet GetCamInfoByCamercaId(Database db, int CamercaId)
        {
            string cmdText = string.Format("select * from CamercaInfo where CamercaId={0} ", CamercaId);
            try
            {
                return db.ExecuteDataSet(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static DataSet GetCamInfoByDeviceId(Database db, int DeviceId)
        {
            string cmdText = string.Format("select * from CamercaInfo where DeviceId={0} order by CamercaId", DeviceId);
            try
            {
                return db.ExecuteDataSet(CommandType.Text, cmdText);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }



    }
}
